package com.shujia.flink.sink

import java.util.concurrent.TimeUnit

import org.apache.flink.api.common.serialization.SimpleStringEncoder
import org.apache.flink.core.fs.Path
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}

object Demo1FileSink {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(1)

    //读取数据

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)


    /**
      * 写入文件
      *
      */
    val sink: StreamingFileSink[String] = StreamingFileSink
      //指定报错路径和数据的格式
      .forRowFormat(new Path("data/flink/out"), new SimpleStringEncoder[String]("UTF-8"))
      .withRollingPolicy(
        DefaultRollingPolicy.builder()
          .withRolloverInterval(TimeUnit.MINUTES.toSeconds(15))
          .withInactivityInterval(TimeUnit.MINUTES.toSeconds(5))
          .withMaxPartSize(1024)
          .build())
      .build()


    linesDS.addSink(sink)


    env.execute()
  }

}
